<?php
 /**
 * Created by PhpStorm.
 * @package    art
 * @copyright  Copyright (c) 2013 LaZaDa SEA
 * @author     long.nguyen-thanh <long.nguyen-thanh@lazada.com
 * @date       6/2/14 3:36 PM
 * @ID         $Id: ValidateEvent.php 83 2014-06-20 10:28:20Z anhkhoa.nguyen2610@gmail.com $,${Id}
 */
class Default_Form_ValidateEvent extends Zendvn_Validate_Form{


    public function __construct($arrParam = null, $options = null){
        $validator = new Zend_Validate();
        $validator->addValidator(new Zend_Validate_NotEmpty(),true);
        if(!$validator->isValid($arrParam['title'])){
            $this->_arrError['title'] = 'Please fill your title at input ';
        }


        /*=======================================================
         * Start - validate picture
        *=======================================================*/

        $upload = new Zend_File_Transfer_Adapter_Http();
        if(!empty($_FILES['image'])){
            $fileInfo = $upload->getFileInfo('image');
            $fileName = $fileInfo['image']['name'];
        }
        if(!empty($fileName)){
            $options = array('gif','png','jpg','jpeg');
            $upload->addValidator('Extension',true,$options,'image')
                   ->addValidator('Size',true,array('min'=>'5kb','max'=>'10000kb'),'image');
            // 					->addValidator('MimeType', false, array('image/gif','image/jpeg','image/png'));
            if(!$upload->isValid('image')){
                $message = $upload->getMessages();
                $this->_arrError['image'] = current($message);
                $arrParam['image'] = '';
            }
        }

        /*=======================================================
         * Start - return correct data array
         *=======================================================*/
        $this->_arrData = $arrParam;
    }

    public function getData(){
        if(!$this->isVaild()){
            $arrFile = $this->upload();
            if(count($arrFile)>0){
                $this->_arrData = array_merge($arrFile,$this->_arrData);
            }
        }
        return $this->_arrData;
    }

    //return true|false
    public function isVaild(){
        $flag = false;
        if(count($this->_arrError) > 0){
            $flag = true;
        }
        return $flag;
    }

    public function getMessageErrors(){
        return $this->_arrError;
    }

    public function upload(){
        $config = new Zendvn_Controller_Config(array('module'=>'default',
                                                     'fileConfig'=>'add-event.ini'));
        $categoryConfig = $config->getConfig('image');
        /*=======================================================
         * Start - upload picture
        *=======================================================*/

        $upload = new Zendvn_File_Upload();
        if(!empty($_FILES['image'])){
            $fileInfo = $upload->getFileInfo('image');
            $fileName = $fileInfo['image']['name'];
        }

        if(!empty($fileName)){
            $options = array('custom_rename'=> true,'uploadDir'=> $categoryConfig['uploadDir']);
            $fileName = $upload->upload($categoryConfig['uploadDir'],'image',$options);
            $arrFile['image'] = $fileName;
            $arrParam = array(
                'uploadDir' => $categoryConfig['uploadDir'],
                'thumb-width' => array($categoryConfig['thumb-width']),
                'thumb-height' => array($categoryConfig['thumb-height']),
                'thumbDir'	=> array($categoryConfig['thumbDir'])
            );
            $options = array('type' => array($categoryConfig['thumbResize']));
            $img = Zendvn_File_Image::resizeImg($fileName, $arrParam, $options);
            if($this->_arrData['action'] == 'edit'){
                $upload->remove($categoryConfig['uploadDir'], $this->_arrData['current_picture']);
                $upload->remove($categoryConfig['thumbDir'], $this->_arrData['current_picture']);
            }
        }
        return @$arrFile;
    }


}







